<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/10/09
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;



/**
 * 钱包提现
 */
class MoneyOut extends Tpmecms
{

    protected $noNeedLogin = ['p'];
    protected $noNeedRight = ['*'];
    protected $db = 'money_out';

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 提现
     *
     * @param float $money 提现金额
     * @param string $pay_type 默认2支付类型:1=微信,2=支付宝,9=其他
     * @param string $alipay_account 支付宝账号为支付宝的时候需要传
     * @return void
     */
    public function money_out($money=0.1,$pay_type='2',$alipay_account=null)
    {
        switch ($pay_type) {
            case '1':
                // $this->error('微信提现暂未开放');
                $this->weixin_money_out($money);
                break;
            case '2':
                $this->alipay_money_out($money,$alipay_account);
                break;
            default:
                $this->error('暂未开放');
                break;
        }
    }

    /**
     * 支付宝提现💰
     *
     * @param float $money 提现金额
     * @return void
     */
    public function alipay_money_out($money=null,$alipay_account=null)
    {
        //判断钱够不够
        if($this->auth->money < $money){
            $this->error('金额不够');
        }
        // CREATE TABLE `fa_money_out` (
        //     `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        //     `uid` int(10) DEFAULT NULL COMMENT '用户id',
        //     `money` decimal(10,2) DEFAULT NULL COMMENT '提现金额',
        //     `pay_time` int(10) DEFAULT NULL COMMENT '到账时间',
        //     `pay_status` enum('1','2','3','4','5','6','7','8','9') DEFAULT '1' COMMENT '支付状态:1=待到账,2=已到账,3=已失败,4=已超时',
        //     `order` varchar(50) DEFAULT NULL COMMENT '订单号',
        //     `time` int(10) DEFAULT NULL COMMENT '提交时间',
        //     `pay_type` enum('1','2','3','9') DEFAULT '1' COMMENT '支付类型:1=微信,2=支付宝,9=其他',
        //     `pay_money` int(10) DEFAULT NULL COMMENT '实际支付（分）',
        //     `pay_openid` varchar(32) DEFAULT NULL COMMENT '支付者openid',
        //     `ps` varchar(255) DEFAULT NULL COMMENT '备注',
        //     `before` decimal(10,2) DEFAULT NULL COMMENT '变更前金额',
        //     `after` decimal(10,2) DEFAULT NULL COMMENT '变更后金额',
        //     `alipay_account` varchar(32) DEFAULT NULL COMMENT '支付宝账号',
        //     PRIMARY KEY (`id`)
        //   ) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8mb4 COMMENT='钱包提现';
        $data['uid'] = $this->auth->id;
        $data['money'] = $money;
        $data['pay_status'] = '1';
        $data['pay_type'] = '2';//支付类型:1=微信,2=支付宝,9=其他'
        $data['alipay_account'] = $alipay_account;
        $res = Db::name($this->db)->where($data)->find();
        if($res){
            $data = $res;
        }else{
            $data['time'] = time();
            $data['order'] = $this->rand_order('MOutw',$this->auth->id,22);
            Db::name($this->db)->insert($data);

        }

        
        $alipay = new \app\api\controller\AlipayOut;
        $alipay->alipay_out_money($data);
        
    }

    /**
     * 微信提现
     *
     * @param int $money
     * @return void
     */
    public function weixin_money_out($money)
    {
        //判断钱够不够
        if($this->auth->money < $money){
            $this->error('金额不够');
        }
        $data['uid'] = $this->auth->id;
        $data['money'] = $money;
        $data['pay_status'] = '1';//支付状态:1=待到账,2=已到账,3=已失败,4=已超时'
        $data['pay_type'] = '1';//支付类型:1=微信,2=支付宝,9=其他'
        $data['pay_openid'] = 'oBsDF5m8lLpLQ89bXsC72ZL8pFww';

        $res = Db::name($this->db)->where($data)->find();
        if($res){
            $data = $res;
        }else{
            $data['time'] = time();
            $data['order'] = $this->rand_order('MOuta',$this->auth->id,22);
            Db::name($this->db)->insert($data);

        }

        $params = $this->get_wxpay_params();
        $sdk = new \Yurun\PaySDK\Weixin\SDK($params);
        $request = new \Yurun\PaySDK\Weixin\CompanyPay\Weixin\Pay\Request;
        $request->partner_trade_no = $data['order']; // 订单号
        $request->openid = $data['pay_openid'];
        $request->check_name = 'NO_CHECK';
        $request->amount = intval($money*100);
        $request->desc = '微信提现';
        // $request->spbill_create_ip = '127.0.0.1';
        $result = $sdk->execute($request);
        var_dump('result:', $result);
        var_dump('success:', $sdk->checkResult());
        var_dump('error:', $sdk->getError(), 'error_code:', $sdk->getErrorCode());
    }

    /**
     * 获取我的提现列表
     *
     * @param int $page 1
     * @param int $limit 10
     * @param string $status 支付状态:1=待到账,2=已到账可不传
     * @return void
     */
    public function get_money_out_list($page=1,$limit=10,$status=null)
    {
        $where['uid']=$this->auth->id;
        if($status){
            $where['pay_status'] = $status;
        }
        $list = Db::name($this->db)->where($where)->page($page,$limit)->select();
        $this->success('ok',$list);
    }




}